package com.jdd.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.AutoLog;
import com.jdd.modules.order.entity.DailyOrderInfo;
import com.jdd.modules.order.entity.ParkOrderInfo;
import com.jdd.modules.order.service.IParkOrderInfoService;
import com.jdd.modules.parking.entity.ParkCarEnterLog;
import com.jdd.modules.parking.entity.ParkCarOutLog;
import com.jdd.modules.parking.service.IparkCarEnterLogService;
import com.jdd.modules.parking.service.IparkCarOutLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
 * @author liuyaowen
 */
@Slf4j
@RestController
@RequestMapping("/dashboard/analysis")
@Api(tags = "首页")
public class DashboardController {

    @Resource
    IParkOrderInfoService parkOrderInfoService;
    @Resource
    IparkCarEnterLogService parkCarEnterLogService;
    @Resource
    IparkCarOutLogService iparkCarOutLogService;
    @Value("${parkInfo.parkCode}")
    private String parkCode;


    /**
     * 首页详细信息查询
     *
     * @param map
     * @return
     */
    @AutoLog(value = "首页-详细信息查询")
    @ApiOperation(value = "首页-首页详细信息查询", notes = "首页-首页详细信息查询")
    @PostMapping(value = "/AnalysisData")
    public Result<Map<String, Object>> orderDetailList(@RequestBody Map<String, Object> map) {
        try {
            Result<Map<String, Object>> result = new Result<>();
            Map<String, Object> jsonArray = new HashMap<>(16);
            CompletableFuture<List<ParkOrderInfo>> payInfo = CompletableFuture.supplyAsync(() -> parkOrderInfoService.orderDetailList());
            CompletableFuture<List<ParkCarEnterLog>> enterInfo =
                    CompletableFuture.supplyAsync(() -> parkCarEnterLogService.enterLogDetails());
            QueryWrapper<ParkCarEnterLog> parkCarEnterLogQueryWrapper = new QueryWrapper<>();
            QueryWrapper<ParkCarOutLog> parkCarOutLogQueryWrapper = new QueryWrapper<>();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            parkCarEnterLogQueryWrapper.ge("car_enter_time", format.format(new Date()));
            parkCarOutLogQueryWrapper.ge("car_out_time", format.format(new Date()));
            CompletableFuture<Integer> dailyEnterCarNumber = CompletableFuture.supplyAsync(() -> parkCarEnterLogService.count(parkCarEnterLogQueryWrapper));
            CompletableFuture<DailyOrderInfo> dailyCharge = CompletableFuture.supplyAsync(() -> parkOrderInfoService.todayOrderDetail());
            CompletableFuture<Integer> dailyOutCarNumber = CompletableFuture.supplyAsync(() -> iparkCarOutLogService.count(parkCarOutLogQueryWrapper));
            jsonArray.put("parkorderinfos", payInfo.get());
            jsonArray.put("EnterInfo", enterInfo.get());
            jsonArray.put("DailyOutCarNumber", dailyOutCarNumber.get());
            jsonArray.put("dailyEnterCarNumber", dailyEnterCarNumber.get());
            jsonArray.put("dailyCharge", dailyCharge.get());
            result.setSuccess(true);
            result.setResult(jsonArray);
            return result;
        } catch (Exception e) {
            Result<Map<String, Object>> result = new Result<>();
            return result.error500(e.getMessage());
        }
    }

    @PostMapping(value = "/GetDaliyOrderInfor")
    public Result<List<DailyOrderInfo>> getDailyOrderInfo(@RequestBody ParkOrderInfo parkOrderInfo) {
        int defaultQueryDays = 15;
        Result<List<DailyOrderInfo>> result = new Result<>();
        parkOrderInfo.setParkCode(parkCode);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<DailyOrderInfo> parkOrderInfoList = parkOrderInfoService.orderDetail(format.format(new Date(System.currentTimeMillis() - 1000 * 24 * 60 * 60 * 14)));
        if (parkOrderInfoList.size() < defaultQueryDays) {
            for (int i = 0; i < defaultQueryDays; i++) {
                Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * i);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
                if (parkOrderInfoList.size() < i + 1) {
                    DailyOrderInfo dailyOrderInfo = new DailyOrderInfo();
                    dailyOrderInfo.setOrderCount(0);
                    dailyOrderInfo.setOrderMount(0);
                    dailyOrderInfo.setCreateTime(simpleDateFormat.format(date));
                    parkOrderInfoList.add(i, dailyOrderInfo);
                } else {
                    if (!parkOrderInfoList.get(i).getCreateTime().equals(simpleDateFormat.format(date))) {
                        DailyOrderInfo dailyOrderInfo = new DailyOrderInfo();
                        dailyOrderInfo.setOrderCount(0);
                        dailyOrderInfo.setOrderMount(0);
                        dailyOrderInfo.setCreateTime(simpleDateFormat.format(date));
                        parkOrderInfoList.add(i, dailyOrderInfo);
                    }
                }
            }
        }
        Collections.reverse(parkOrderInfoList);
        result.setResult(parkOrderInfoList);
        return result;
    }

}
